package com.spzx.channel.service.impl;

import com.alibaba.fastjson2.JSON;
import com.spzx.channel.service.ItemService;
import com.spzx.common.core.constant.Constants;
import com.spzx.common.core.constant.SecurityConstants;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.ProductDetailsFeignClient;
import com.spzx.product.api.ProductFeignClient;
import com.spzx.product.api.ProductSkuFeignClient;
import com.spzx.product.api.SkuStockFeignClient;
import com.spzx.product.api.domain.Product;
import com.spzx.product.api.domain.ProductDetails;
import com.spzx.product.api.domain.ProductSku;
import com.spzx.product.api.domain.SkuPrice;
import com.spzx.product.api.domain.vo.ItemVo;
import com.spzx.product.api.domain.vo.SkuStockVo;
import jakarta.annotation.Resource;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName ItemServiceImpl
 * @Author 波
 * @Date 2024/8/5 19:38
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Resource
    private ProductSkuFeignClient productSkuFeignClient;
    @Resource
    private SkuStockFeignClient skuStockFeignClient;
    @Resource
    private ProductFeignClient productFeignClient;
    @Resource
    private ProductDetailsFeignClient productDetailsFeignClient;
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public ItemVo getItem(Long skuId) {
        //先通过布隆过滤器看集合中是否有skuId，没有直接抛出异常
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(Constants.PRODUCT_BLOOM_FILTER_NAME);
        if (!bloomFilter.contains(skuId)) {
            throw new ServiceException(skuId + "号商品价格数据不存在");
        }
        ItemVo itemVo = new ItemVo();

        //获取sku信息
        CompletableFuture<ProductSku> productSkuCompletableFuture = CompletableFuture.supplyAsync(() -> {
            R<ProductSku> productSkuResult = productSkuFeignClient.getProductSku(skuId, SecurityConstants.INNER);
            if (R.FAIL == productSkuResult.getCode()) {
                throw new ServiceException(productSkuResult.getMsg());
            }
            ProductSku productSku = productSkuResult.getData();
            itemVo.setProductSku(productSku);
            return productSku;
        }, threadPoolExecutor);
        //获取商品信息
        CompletableFuture<Void> productCompletableFuture = productSkuCompletableFuture.thenAcceptAsync((productSku) -> {
            R<Product> productResult = productFeignClient.selectProductById(productSku.getProductId(), SecurityConstants.INNER);
            if (R.FAIL == productResult.getCode()) {
                throw new ServiceException(productResult.getMsg());
            }
            Product product = productResult.getData();
            itemVo.setProduct(product);
            itemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls().split(",")));
            itemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        }, threadPoolExecutor);
        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(() -> {
            //获取商品最新价格
            R<SkuPrice> skuPriceResult = productSkuFeignClient.getSkuPrice(skuId, SecurityConstants.INNER);
            if (R.FAIL == skuPriceResult.getCode()) {
                throw new ServiceException(skuPriceResult.getMsg());
            }
            SkuPrice skuPrice = skuPriceResult.getData();
            itemVo.setSkuPrice(skuPrice);
        }, threadPoolExecutor);

        //获取商品详情
        CompletableFuture<Void> productDetailsCompletableFuture = productSkuCompletableFuture.thenAcceptAsync((productSku) -> {
            R<ProductDetails> productDetailsResult = productDetailsFeignClient.details(productSku.getProductId(), SecurityConstants.INNER);
            if (R.FAIL == productDetailsResult.getCode()) {
                throw new ServiceException(productDetailsResult.getMsg());
            }
            ProductDetails productDetails = productDetailsResult.getData();
            itemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls().split(",")));
        }, threadPoolExecutor);

        //获取商品规格对应商品skuId信息
        CompletableFuture<Void> skuSpecValueCompletableFuture = productSkuCompletableFuture.thenAcceptAsync((productSku) -> {
            R<Map<String, Long>> skuSpecValueResult = productSkuFeignClient.getSkuSpecValueMap(productSku.getProductId(), SecurityConstants.INNER);
            if (R.FAIL == skuSpecValueResult.getCode()) {
                throw new ServiceException(skuSpecValueResult.getMsg());
            }
            Map<String, Long> skuSpecValueMap = skuSpecValueResult.getData();
            itemVo.setSkuSpecValueMap(skuSpecValueMap);
        }, threadPoolExecutor);

        //获取商品库存信息
        CompletableFuture<Void> skuStockCompletableFuture = CompletableFuture.runAsync(() -> {
            R<SkuStockVo> skuStockResult = skuStockFeignClient.getSkuStockVo(skuId, SecurityConstants.INNER);
            if (R.FAIL == skuStockResult.getCode()) {
                throw new ServiceException(skuStockResult.getMsg());
            }
            SkuStockVo skuStockVo = skuStockResult.getData();
            itemVo.setSkuStockVo(skuStockVo);
        }, threadPoolExecutor);
        CompletableFuture.allOf(
                productSkuCompletableFuture,
                productCompletableFuture,
                skuPriceCompletableFuture,
                productDetailsCompletableFuture,
                skuSpecValueCompletableFuture,
                skuStockCompletableFuture
        ).join();
        return itemVo;
    }
}
